package com.wsd.smartcarwasher.dao;

import com.wsd.smartcarwasher.domain.OrderInfo;
import com.wsd.smartcarwasher.domain.OrderStatistics;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.shiro.dao.DataAccessException;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface StatisticDao  {

    List<OrderStatistics> queryTurnoverTrend(@Param("dateFormat") String dateFormat,
                                             @Param("machineId") String machineId,
                                             @Param("partnerId") Integer partnerId,
                                             @Param("createTime") String createTime,
                                             @Param("format") Integer format) throws DataAccessException;

    List<OrderStatistics> queryTurnoverTrendMonth(@Param("dateFormat") String dateFormat,
                                             @Param("machineId") String machineId,
                                             @Param("partnerId") Integer partnerId,
                                             @Param("createTime") String createTime,
                                                  @Param("format") Integer format) throws DataAccessException;
    List<OrderStatistics> queryTurnoverTrend30Day(@Param("dateFormat") String dateFormat,
                                                  @Param("machineId") String machineId,
                                                  @Param("partnerId") Integer partnerId,
                                                  @Param("createTime") String createTime,
                                                  @Param("startTime") String startTime,
                                                  @Param("endTime") String endTime,
                                                  @Param("format") Integer format) throws DataAccessException;
    List<OrderStatistics> queryTurnoverTrendOfMachines(@Param("dateFormat") String dateFormat,
                                                       @Param("machineIds") List<Integer> machineIds,
                                                       @Param("partnerId") Integer partnerId,
                                                       @Param("time") String time
                                                       ) throws DataAccessException;

    /**
     * 查询订单信息
     * @param machineId 如果机器id为空，则查询所有订单信息
     * @return
     * @throws DataAccessException
     */
    List<OrderInfo> queryOrderInfoByMachineId(@Param("machineId") String machineId,
                                              @Param("partnerId") Integer partnerId) throws DataAccessException;

    List<OrderInfo> queryOrderInfoByMachineIds(@Param("machineIds") List<Integer> machineIds,
                                               @Param("partnerId") Integer partnerId) throws DataAccessException;

    List<OrderInfo> queryWashOrder(String userId,String startTime) throws DataAccessException;

    Map<String,Object> queryOnline(String sTime, String dTime, int serviceId, Integer partnerId,Integer machineId);

    List<OrderInfo> queryUserOrder(String userId) throws DataAccessException;

    OrderInfo queryOrderInfoByOrderSn(@Param("orderSn") String orderSn) throws DataAccessException;
    /**
     *
     * @return
     */
    Map<String,Object> queryBackFlowCustomer(String sTime,String BackFlowTime,String eTime,String Customers);
    List<OrderInfo> queryOrderCustomer(String sTime);
    Map<String,Object> getAllNumberStatistics();
    Map<String,Object> getStoreNumberStatistics(int partnerId);
    //数据总览

    Map<String,Object> getAllNumberView(String startTime,String endTime,Integer partnerId);
    //商家端数据总览
    Map<String,Object> getAllNumberViewByPartnerId(String startTime,String endTime,int partnerId);


    Map<String,Object> getCustomerUserid(String sTime,String eTime,Integer partnerId,Integer backflowDays,Integer flybackDays);



//新用户数量
    @Select("SELECT  count(0) as newCustomer FROM t_user where \n" +
            "t_user.userId in(\n" +
            "\n" +
            "(select\n" +
            "      userId\n" +
            "        FROM\n" +
            "            t_order  \n" +
            "        WHERE\n" +
            "            payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} \n" +
            "        GROUP BY\n" +
            "            userId)) and t_user.registerTime BETWEEN #{sTime} and  #{eTime} ")
    Map<String,Object> getNewCustomer(String sTime,String eTime);
//所有用户数量
    @Select("select\n" +
            "     count(DISTINCT userId) as allCustomer\n" +
            "        FROM\n" +
            "            t_order  \n" +
            "        WHERE\n" +
            "            payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} \n" +
            "        ORDER BY\n" +
            "            userId")
    Map<String,Object> getallCustomer(String sTime,String eTime);
    //新用户的交易金额
    @Select("SELECT  count(t_order.orderSn) as newTraderNumber,sum(t_order.payPrice) as newCustomerPrice FROM t_order where \n" +
            "\n" +
            "t_order.userId in(\n" +
            "\n" +
            "(\t\t\t\n" +
            "\t\t\t\t\t\tSELECT  userId FROM t_user where \n" +
            "            t_user.userId in(\n" +
            "       \n" +
            "            (select\n" +
            "                 userId\n" +
            "                   FROM\n" +
            "                       t_order  \n" +
            "                    WHERE\n" +
            "                        payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} \n" +
            "                    GROUP BY\n" +
            "                        userId)) and t_user.registerTime BETWEEN #{sTime} and  #{eTime} ) )and payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} ")
    Map<String,Object> getNewCustomerPrice(String sTime,String eTime);
    //所有用户的交易金额
    @Select("\n" +
            "SELECT  count(t_order.orderSn) as allTraderNumber,sum(t_order.payPrice) as allCustomerPrice FROM t_order where \n" +
            "t_order.userId in(\n" +
            "\n" +
            "(select\n" +
            "      userId\n" +
            "        FROM\n" +
            "            t_order  \n" +
            "        WHERE\n" +
            "            payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} \n" +
            "        GROUP BY\n" +
            "            userId)) and payStatus=2 and startTime BETWEEN #{sTime} and  #{eTime} ")
    Map<String,Object> getAllCustomerPrice(String sTime,String eTime);


//    @Select("SELECT COUNT(DISTINCT o.userId) as newcustomer,sum(o.payPrice) as tradePrice\n" +
//            "FROM t_order o\n" +
//            "\n" +
//            "LEFT JOIN t_user t\n" +
//            "\n" +
//            "on o.userId=t.userId\n" +
//            "\n" +
//            "where o.payStatus=2 and  t.registerTime BETWEEN #{sTime} and  #{eTime}  ")
//    Map<String,Object> getTradePoportion(String sTime,String eTime);

    List<Map<String,Object>> getAllOrderTop10();


    List<OrderStatistics> queryTurnoverTrendByIndex(@Param("machineId")Integer machineId, @Param("partnerId")Integer partnerId, @Param("sTime") Date sTime, @Param("eTime") Date eTime);

    List<OrderInfo> queryOrderInfoByMachineIdsByRu(@Param("machineIds") List<Integer> machineIds,@Param("partnerId") Integer id);



}
